<template>
  <div class="home">
    <div class="header">
      <h1 class="header-title">恭喜您，话费券名额已锁定!</h1>
      <div class="logo"><img src="../../../assets/img/hyh.png" /></div>
      <div class="time">
        <div>请在 </div>
        <van-count-down :time="time">
          <template #default="timeData">
            <span class="block">{{ timeData.minutes ? timeData.minutes : '00' }}</span>
            <span class="block">分</span>
            <span class="block">{{ timeData.seconds }}</span>
            <span class="block">秒</span>
          </template>
        </van-count-down>
        <div> 内完成支付</div>
      </div>
      <div class="header-btn">
        <span @click="fnClickRules">规则</span>
        <span @click="fnClickPe">客服</span>
      </div>
    </div>
    <div class="content">
      <van-cell-group inset>
        <van-field v-model="phoneNumber" name="手机号"  placeholder="请输入手机号"  right-icon="phone" ref="inputNumber" maxlength="11"
        autocomplete="off" type="number" @update:model-value="onChange"  size="large"/>
      </van-cell-group>
      <div class="pay-title">
        <div class="title">充值话费</div>
        <div>（全国三网通用，领不到可退）</div>
      </div>
      <div class="money">
        <div class="money—public">
          <div>
            <span class="money-num">20</span>
            <span>元</span>
          </div>
          <div class=" public-margin">售价：20元</div>
        </div>
        <div class="money—specail">
          <div class="triangle"></div>
          <span class="triangle-title">推荐</span>
          <div>
            <span class="money-num" style="color:#fff">100</span>
            <span>元</span>
          </div>
          <div class=" public-margin">实付：{{ amount }}元</div>
        </div>
        <div class="money—public">
          <div>
            <span class="money-num">50</span>
            <span>元</span>
          </div>
          <div class="public-margin">售价：50元</div>
        </div>
      </div>
    </div>
    <div class="pay">
      <div class="title">支付方式<span class="title-red">(随机立减)</span> </div>
      <div class="payment">
        <!-- <div> -->
        <van-radio-group v-model="checkedPay" direction="horizontal">
          <van-radio name="2"> <img src="../../../assets/img/wxlogo.png" alt="" />微信</van-radio>
          <!-- <van-radio name="1">
            <img src="../../../assets/img/logo.png" alt="" />支付宝
          </van-radio> -->
        </van-radio-group>
      </div>
      <div class="public-margin">
        <span>实际支付金额：</span>
        <span class="public-red">{{ amount }} 元</span>
      </div>
      <div class="public-margin">
        <span>实际到账话费卷：</span>
        <span class="public-red">100 元</span>
      </div>
      <div class="submit" @click="onSubmit">
        立即充值
      </div>
      <!-- <div class="special-num">待结省金额：<span class="public-red">{{100 - amount}}元</span></div> -->
      <div class="flex  agree ">
        <van-checkbox v-model="checked" shape="square" class="public-right"></van-checkbox>
        提交视为已阅读并同意
        <span class="public-color" @click="fnclickPrivacy"> 《隐私政策》</span>
        <!-- <span class="public-color" @click="fnclickAgreen"> 《用户协议》</span> -->
      </div>
    </div>
    <div class="desc">
      <div class="desc-title">话费优惠券包说明</div>
      <div v-for="(item, index) in desc" :key="index" class="desc-content">
        {{ item }}
      </div>
    </div>
    <div class="footer">
      <!-- <div>秦皇岛五爪螺科技有限公司</div> -->
      <div>海南马拉多纳科技有限公司</div>
      <div class="gray">冀ICP备202311132号-3</div>
      <div><small data-v-a9f2f654="">客服电话：<a class="tel" href="tel:0516-53764328"
            data-v-a9f2f654="">0516-53764328</a></small></div>
    </div>
    <!-- 活动规则弹窗 -->
    <van-popup v-model:show="show" :style="{ height: '70%' }" closeable close-icon="close" round class="rules">
      <div class="rules-title">活动规则</div>
      <div v-for="(item, index) in activityContent" :key="index" class="desc-content">
        <div v-html="item"></div>
      </div>
    </van-popup>
  </div>
</template>
<script>
/* eslint-disable*/
import { ref, reactive ,getCurrentInstance,onMounted} from 'vue'
import { useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'
import api from '../api/index'
import { Base64 } from 'js-base64';

const urlController = (url) => {
      var _url = url.split('?')[1]
      if (!_url) {
        return {}
      }
      var windowHref = _url.split('&')
      var obj = {}
      for (var i = 0; i < windowHref.length; i++) {
        var arr = windowHref[i].split('=')
        obj[arr[0]] = arr[1]
      }
      return obj
    }

export default {
  name: 'Home',
  components: {
    // HelloWorld
  },
  setup () {
    const router = useRouter()
    console.log(urlController(location.href),"3333")
    const time = ref(5 * 60 * 1000)
    const phone = urlController(location.href)?.phone && Base64.decode(urlController(location.href)?.phone) || ''
    const code = urlController(location.href)?.qcjParamStr  || ''
    const phoneNumber =  ref(phone)
    
    const checked = ref(1)
    const checkedPay = ref('2')
    const amount = '29.' + (Math.round(Math.random() * (9)) + 1)
    const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    const desc = reactive([
      '1、开通权益的会员，可享专属话费权益：100元话费优惠券包包含：5张满200减10元，10张满100减5元',
      '2、话费充值金额包含：50元、100元、200元，话费券每天不限使用，支持全国移动、联通、电信三网充值，暂不支持16 17 19开头的虚拟号以及携号转网的手机号充值；',
      '3、话费优惠券有效期为1年，话费充值到账时间为72小时内，以当地运营商处理时间为准。'
    ])
    // 活动规则
    const show = ref(false)
    const activityContent = reactive([
      '以下权益仅对于本次活动中购买会员的用户专有，在好惠省APP中使用：',
      '1、本活动用户支付后即可获得价值100元话费充值优惠券；',
      '2、100元话费优惠券包含：5张满200减10元，10张满100减5元，优惠券单次充值使用一张；',
      '3、专属话费优惠券的使用：用户可在下单短信内，点击充值链接<a href="https://www.qhdyywlkj.com/download/app.html">（下载“好惠省”APP）</a>-点击优惠充值-用购买券包的手机号登录APP领取话费优惠券-选择充值金额-选择优惠券扣减-完成话费优惠充值； ',
      '4、话费充值正常为72小时内到账，如遇高峰时段（月初/月末/节假日），到账时间可能会略有延迟，具体到账时间以当地运营商处理时间为准，请耐心等待，到账后会收到手机短信提醒，如果超时未到可<a href="https://1587819.s4.udesk.cn/im_client/?web_plugin_id=48558">联系客服</a>处理。</span>',
      '5、使用APP充值话费，优惠充值1年内有效；',
      '6、由于“好惠省”APP暂不支持IOS系统手机安装，因此苹果手机用户无法获取本权益。',
      '7、如有疑问，可点<a href="https://1587819.s4.udesk.cn/im_client/?web_plugin_id=48558">联系客服</a>进行咨询。'

    ])
    let currentInstance = ''

        onMounted(() => {
            currentInstance = getCurrentInstance()
        })
    const fnClickRules = () => {
      show.value = true
    }
    const fnclickPrivacy = () => {
      router.push('/privacyAgreement')
    }
    const fnclickAgreen = () => {
      router.push('/agree')
    }
    const fnClickPe = () => {
      window.open('https://1587819.s4.udesk.cn/im_client/?web_plugin_id=48558')
    }
    // 提交按钮
    const onSubmit = () => {
      let ua = window.navigator.userAgent
      if(!!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){
       // ios端 
       showToast('暂不支持')
        return
   }
      if (!phoneNumber.value) { 
        showToast('请输入手机号')
        currentInstance.proxy.$refs.inputNumber.focus()
        return
      }
      if (!reg_tel.test(phoneNumber.value)) {
        showToast('请输入正确手机号')
        return
      }
      if (!checked.value) {
        showToast('请勾选下方用户协议隐私协议')
        return
      }
      getPaw()
    }
    const onChange = () => {
      if(phoneNumber.value.length === 11){
        currentInstance.proxy.$refs.inputNumber.blur()
      }
    }
    const getPaw = () => {
      api.get('getOutTradeNo', { phone: phoneNumber.value }).then(res => {
        if (res.code === 200) {
          const { count, orderNo } = res.data
          if (!count) {
            showToast('您的购买已到达上限')
            return
          }
          // const data = {
          //   amount: '29.9',
          //   outTradeNo: res.data || '',
          //   phoneNumber: phoneNumber.value,
          //   type: 'adapay'
          // }
          // checkedPay 根据选择的支付方式确认走支付宝还是微信逻辑
          if (checkedPay.value === '1') {
            window.open('https://www.qhdyywlkj.com/api/tenant/payment/wapPay?amount=' + amount + '&type=alipay&outTradeNo=' + orderNo + '&phoneNumber=' + phoneNumber.value, '_blank')
          } else {
            getWxlink(orderNo)
          }
          // api.post('wapPay', { ...data }).then(res => {
          //   console.log(res)
          //   if (res.code === 200) {
          //     // window.open('https://www.baidu.com')
          //     // this.phoneNum = ''
          //     // this.$store.commit('TOAST', '添加成功!')
          //   } else {
          //     showToast(res.message)
          //   }
          // })
          // setTimeout(() => {
          //   showConfirm(res.data)
          // }, 1000)
        } else {
          showToast(res.message)
        }
      })
    }
    const showConfirm = (outTradeNo) => {
      showConfirmDialog({
        title: '支付确认',
        confirmButtonText: '已支付完成',
        cancelButtonText: '未支付',
        message:
          '您发起的订单支付是否已经支付完成。'
      })
        .then(() => {
          router.push({
            path: '/result',
            query: {
              outTradeNo: outTradeNo,
              amount: amount,
              phoneNumber: phoneNumber.value
            }
          })
        })
        .catch(() => {
          // on cancel
          router.push({
            path: '/result',
            query: {
              outTradeNo: outTradeNo,
              amount: amount,
              phoneNumber: phoneNumber.value
            }
          })
        })
    }
    const getWxlink = (outTradeNo) => {
      const params = {
        outTradeNo: outTradeNo,
        amount: amount,
        type: 'kala',
        phoneNumber: phoneNumber.value,
        code:code
      }
      api.get('getWxLink', { ...params }).then(res => {
        if (res.code  * 1=== 200) {
          localStorage.setItem("wxlink",res.data);
          location.href =res.data
          // window.open(res.data, '_blank')
        } else {
          router.push({
            path: '/result',
            query: {
              outTradeNo: outTradeNo,
              amount: amount,
              phoneNumber: phoneNumber.value,
              type: 'kala',
            }
          })
        }
      })
    }
   
    return {
      time,
      phoneNumber,
      checked,
      checkedPay,
      amount,
      desc,
      show,
      activityContent,
      fnClickRules,
      fnclickPrivacy,
      fnclickAgreen,
      fnClickPe,
      onSubmit,
      getPaw,
      showConfirm,
      getWxlink,
      onChange
      // urlController
    }
  }
}
</script>
<style lang="scss" scoped>
.home {
  font-size: .14rem;
  color: #000;
  .title-red{
    font-size:0.08rem;
    color:red;
  }
  .header {
    color: #fff;
    height: 1rem;
    background: #1829D8;
    border-bottom-left-radius: 0.1rem;
    border-bottom-right-radius: 0.1rem;

    &-title {
      font-weight: 700;
      color: #fff;
      padding-top: 0.3rem;
    }

    .logo {
      position: absolute;
      left: .05rem;
      top: .05rem;

      img {
        height: .25rem;
      }
    }

    .time {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: .1rem;
      padding-top: .1rem
    }

    &-btn {
      position: absolute;
      right: 0;
      top: .5rem;
      font-size: .05rem;
      display: flex;
      flex-direction: column;

      span {
        display: inline-block;
        background: #0B1052;
        padding: 0.05rem 0.08rem;
        border-top-left-radius: 0.05rem;
        border-bottom-left-radius: 0.05rem;
        margin-bottom: 0.1rem;
        z-index: 999;
      }
    }

    .block {
      display: inline-block;
      width: 18px;
      font-size: 14px;
      color: #FED20C;
      text-align: center;
      border-radius: 0.04rem;
      font-weight: bold;
    }
  }

  .content {
    margin: 0 4%;
    margin-top: -.15rem;
    padding: 0.05rem;
    border-radius: .1rem;
    background: #fff;
    background-size: 100% 100%;

    /deep/.van-cell {
      background-color: #f8f8f8 !important;
      border-radius: .1rem;
    }

    /deep/.van-field__control::-webkit-input-placeholder {
      color: #333;
      font-size: 0.12rem;
    }
    /deep/.van-icon-phone{
      font-size:0.12rem;
      color:blue
    }
    .title {
      color: #000;
      text-align: left;
      font-size: .12rem;
      margin: .12rem 0;
    }

    .money {
      font-size: .12rem;
      color: #000;
      display: flex;
      justify-content: space-around;

      &—public {
        margin: 0 5px;
        border: 1px solid #ddd;
        padding: .15rem .1rem;
        font-size: .05rem;
        border-radius: .05rem;
        background: #EEF8FE;
      }

      &—specail {
        position: relative;
        margin: 0 5px;
        border: 1px solid #1576ff;
        border-radius: .05rem;
        padding: .15rem .1rem;
        font-size: .05rem;
        background: linear-gradient(90deg, #279eff, #2787ff);
        color: #fff;
      }

      &-color {
        color: #1576ff;
      }

      &-margin {
        margin-top: .1rem;
      }

      &-num {
        color: rgb(32, 32, 32);
        letter-spacing: 1px;
        font-weight: 700;
        font-size: .2rem;
      }
    }
  }

  .pay-title {
    display: flex;
    align-items: center;

    div:nth-child(2) {
      font-size: 0.08rem;
      color: #666;
      margin-left: .02rem;
    }
  }

  .pay {
    background: #fff;
    margin: .05rem 4% 0 4%;
    text-align: left;
    padding: .05rem;
    font-size: .1rem;
    border-radius: .1rem;

    .agree {
      font-size: .08rem;
      line-height: .2rem;
    }

    .payment {
      line-height: .15rem;

      /deep/.van-radio__label {
        display: flex;
        align-items: center;
      }

      div:nth-child(1) {
        display: flex;

        img {
          width: .15rem;
          margin-right: .05rem;
        }
      }

      display: flex;
      justify-content: space-between;
    }
  }

  .desc {
    margin: .05rem 4% 0 4%;
    border-radius: .1rem;
    background: #fff;

    .desc-title {
      font-size: .14rem;
      text-align: center;
      font-weight: 700;
      color: #666;
      padding: .05rem;
    }

    .desc-content {
      padding: 0 .08rem;
      line-height: .2rem;
      text-align: left;
      margin: .05rem 0;
      font-size: .05rem;
    }
  }

  .footer {
    margin: .1rem;
    font-size: .05rem;
    color: #717171;

    .gray {
      color: rgb(217, 217, 217);
      margin: .05rem 0;
    }
  }

  /deep/.van-popup--center {
    padding: .1rem;
    padding-top: .2rem;
  }

  .rules {
    .rules-title {
      text-align: center;
      font-weight: 700;
      font-size: .12rem;
      margin-bottom: .1rem;
    }

    div {
      line-height: .15rem;
      text-align: left;
      font-size: .05rem;
      margin-bottom: .15rem;
    }
  }

  .submit {
    width: 100%;
    position: relative;
    background: linear-gradient(90deg, #279eff, #2787ff);
    box-shadow: 0 0.12rem 0.16rem 0 rgba(46, 78, 253, .15);
    color: white;
    text-align: center;
    height: .3rem;
    font-size: .24rem;
    line-height: .3rem;
    border-radius: 3rem;
    margin: .2rem auto;
    padding: .05rem;
    -webkit-animation: bouncedelay 1s infinite linear;
    animation: bouncedelay 1s infinite linear;
  }

  .special-num {
    text-align: left;
    margin-top: .1rem;
    font-weight: bold;
    font-size: .08rem;
  }

  .public-color {
    color: #1576ff;
  }

  .public-red {
    color: #2787ff;
    font-size: 0.14rem;
    font-weight: 700;
  }

  .public-margin {
    white-space:nowrap;
    margin-top: .1rem;
  }

  .public-margin5 {
    margin-top: .05rem;
  }

  .public-margin15 {
    margin-top: .15rem;
  }

  .public-right {
    margin-right: .03rem;
  }

  .title {
    color: #000;
    text-align: left;
    font-size: .12rem;
    margin: .12rem 0;
    font-weight: 700;
  }

  .flex {
    display: flex;
  }

  .red {
    color: red;
  }

  .triangle {
    position: absolute;
    top: -1px;
    left: -1px;
    width: 0;
    height: 0;
    border-top: .3rem solid red;
    border-right: .3rem solid transparent;
  }

  .triangle-title {
    color: #fff;
    position: absolute;
    top: 0.05rem;
    left: 0;
    transform: rotate(-45deg);
  }
}

@keyframes bouncedelay {

  0%,
  100% {
    transform: scale(0.9);
  }

  50% {
    transform: scale(0.8);
  }
}</style>
